const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    code:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this,scene = decodeURIComponent(options.scene)
    console.log(scene.split('=')[1])
    if(wx.getStorageSync('ACCESS_TOKEN')){
      _this.setData({code: scene.split('=')[1]})
      _this.getOrderDetail()
    }else {
      App.args.page.onLoad(_this, options)
    }
    
  },

  /**
   * 获取订单详情
   */
  getOrderDetail() {
    let _this = this
    wx.showLoading({title: '加载中',mask:true})
    App.args.request({
      url: App.args.api.siteinfo.sitelive + 'integral/jfqrcode',
      data:{
          storeId: App.args.api.siteinfo.hosId2,
          openId:wx.getStorageSync('ACCESS_TOKEN'),
          recordId:_this.data.code
      },
      success(e){
        console.log(e)
        if(e.result == 0) {
          App.args.request({
            url: App.args.api.siteinfo.sitelive + 'integral/goodsDetail',
            data:{
                storeId: App.args.api.siteinfo.hosId2,
                goods_id:e.data[0].goods_id
            },
            success(res){
              console.log(res)
              if(res.code == 0) {
                let order = Object.assign(res , {
                  shop_name:App.args.api.siteinfo.name,
                  shop_address:App.args.api.siteinfo.address,
                  status:e.data[0].status,
                  name:e.data[0].name,
                  create_time:e.data[0].create_time,
                })
                _this.setData({
                  order:order
                })
                wx.hideLoading()
              }
            }
          })
        }
      }
    })
  },

  /**
   * 查看位置
   */
  onOpenLocation() {
    wx.openLocation({
      name: App.args.api.siteinfo.name,
      address: App.args.api.siteinfo.address,
      longitude: App.args.api.siteinfo.longitude,
      latitude: App.args.api.siteinfo.latitude,
      scale: 15
    })
  },

  /**
   * 确认核销
   */
  onSubmitExtract() {
    let _this = this
    wx.showModal({
      title: "提示",
      content: "确认核销该订单吗？",
      success(o) {
        if (o.confirm) {
          App.args.request({
            url: App.args.api.siteinfo.sitelive + 'integral/hexiao',
            data:{
              recordId:_this.data.code,
              hosId:App.args.api.siteinfo.acid,
              clerk_open_id:wx.getStorageSync('ACCESS_TOKEN')
            },
            success(e){
                console.log(e)
              if(e.result < 0) {
                App.showError(e.message, () => {
                  //_this.getOrderDetail();
                })
              }else{
                App.showSuccess(e.message, () => {
                  App.navigationTo('pages/index/index') 
                  wx.exitMiniProgram()
                })
              }
            }
          })
        }
      }
    })
  },
})